<template>
  <vxe-modal @close="cancel" title="选择设备" v-model="sbDuox" width="85%" append-to-body showFooter show-zoom resize>
    <div style="height: 75vh; overflow-y: auto;">
      <!-- 顶部搜索 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="设备名称" prop="sbName">
          <el-input size="mini"
                    v-model="queryParams.sbName"
                    placeholder="请输入设备名称"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="出厂编号" prop="ccBh">
          <el-input size="mini"
                    v-model="queryParams.ccBh"
                    placeholder="请输入出厂编号"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="自编号" prop="zbh">
          <el-input size="mini"
                    v-model="queryParams.zbh"
                    placeholder="请输入自编号"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="固定资产编号" prop="gdZcBh">
          <el-input
            v-model="queryParams.gdZcBh"
            placeholder="请输入固定资产编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="校准周期" prop="jzZq">
          <el-input size="mini"
                    v-model="queryParams.jzZq"
                    placeholder="请输入校准周期"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="规格型号" prop="ggXh">
          <el-input size="mini"
                    v-model="queryParams.ggXh"
                    placeholder="请输入规格型号"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="所在项目" prop="szXm">
          <el-input size="mini"
                    v-model="queryParams.szXm"
                    placeholder="请输入所在项目"
                    clearable
                    @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备类型" prop="sbLx">
          <el-select size="mini" v-model="queryParams.sbLx" placeholder="请选择设备类型" clearable>
            <el-option
              v-for="dict in dict.type.sbgl_sblx"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="有效期至">
          <el-date-picker size="mini"
                          v-model="daterangeYxqZ"
                          style="width: 240px"
                          value-format="yyyy-MM-dd"
                          type="daterange"
                          range-separator="-"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table height="55vh" @row-click="clickRow" ref="table" highlight-current-row
                :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                v-loading="loading" :data="sbxxglList" @selection-change="handleSelectionChange">
<!--        <el-table-column type="selection" width="55" align="center"/>-->
        <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
        <el-table-column label="设备状态" align="center" prop="sbZt">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sbgl_sbzt" :value="scope.row.sbZt"/>
          </template>
        </el-table-column>
        <el-table-column label="设备名称" align="center" prop="sbName" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleButtonClick(scope.row)"
            >
              <el-link type="primary">{{ scope.row.sbName }}</el-link>
            </el-button>
          </template>
        </el-table-column>
<!--        <el-table-column label="二维码" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <div>-->
<!--              <ImagePreviewBase64 :content="scope.row.sbxxglId + ':sbm'"/>-->
<!--            </div>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="出厂编号" align="center" prop="ccBh" show-overflow-tooltip/>
        <el-table-column label="自编号" align="center" prop="zbh" show-overflow-tooltip/>
        <el-table-column label="固定资产编号" align="center" prop="gdZcBh" show-overflow-tooltip/>
        <el-table-column label="校准周期" align="center" prop="jzZq" show-overflow-tooltip/>
        <el-table-column label="规格型号" align="center" prop="ggXh" show-overflow-tooltip/>
        <el-table-column label="准确度/不准确度" align="center" prop="zqdBzqd" show-overflow-tooltip/>
        <el-table-column label="所在项目" align="center" prop="szXm" show-overflow-tooltip/>
        <el-table-column label="单位" align="center" prop="dw" show-overflow-tooltip/>
        <el-table-column label="生产厂家" align="center" prop="scCj" show-overflow-tooltip/>
        <el-table-column label="设备类型" align="center" prop="sbLx" show-overflow-tooltip>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sbgl_sblx" :value="scope.row.sbLx"/>
          </template>
        </el-table-column>
        <el-table-column label="测量范围" align="center" prop="clFw" show-overflow-tooltip/>
        <el-table-column label="有效期至" align="center" prop="yxqZ" width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.yxqZ, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="购置日期" align="center" prop="gzRq" width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.gzRq, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

    </div>

    <div slot="footer" class="dialog-footer dialog-footer-center">
      <el-button size="small" @click="submitForm">
        <svg-icon icon-class="confirm1" class-name='custom-class' />确 定
      </el-button>
      <el-button size="small" @click="cancel">
        <svg-icon icon-class="cancel1" class-name='custom-class' />取 消
      </el-button>
    </div>

    <!-- 设备详情 -->
    <SbXq ref="sbXq"/>
  </vxe-modal>
</template>

<script>
import ImagePreviewBase64 from "@/components/ImagePreviewBase64/index.vue";
import {listSbxxgl} from "@/api/sbgl/sbxxgl";
import SbXq from "@/views/sbgl/sbxxgl/components/SbXq.vue";

export default {
  name: "sbDX", //设备单选 (设备状态为设备停用)
  components: {SbXq, ImagePreviewBase64},
  dicts: ['sbgl_sblx', 'sbgl_sbdw', 'sbgl_sbzt'],
  inject: ['updateSb'],
 //设备多选组件
  data() {
    return {
      // 设备信息管理表格数据
      sbxxglList: [],
      // 遮罩层
      loading: true,
      sbDuox: false, //弹窗是否打开
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        idWhere: null,
        sbName: null,
        ccBh: null,
        zbh: null,
        jzZq: null,
        ggXh: null,
        szXm: null,
        sbLx: null,
        yxqZ: null,
        userId: null,
        deptId: null,
        gdZcBh: null,
        sbZt: '停用',
      },
      // 显示搜索条件
      showSearch: true,
      sbLable: null, //最后选择的设备
      // 总条数
      total: 0,
      // 文件大小时间范围
      daterangeYxqZ: [],
      xzSbList: [], //选中的设备列表
      allSelected: [], // 存储所有选中的行
      // 选中数组
      ids: [],
    }
  },
  methods: {
    handleOpen(){
      this.queryParams.pageNum = 1
      this.sbDuox = true;
      this.getList();
    },
    /** 查询设备信息管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeYxqZ && '' != this.daterangeYxqZ) {
        this.queryParams.params["beginYxqZ"] = this.daterangeYxqZ[0];
        this.queryParams.params["endYxqZ"] = this.daterangeYxqZ[1];
      }
      listSbxxgl(this.queryParams).then(response => {
        this.sbxxglList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.sbDuox = false;
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
      this.sbLable = this.selectedRow
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeYxqZ = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.sbxxglId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    // 查询设备详情信息
    handleButtonClick(row) {
      this.$refs.sbXq.handleOpen(row);
    },
    submitForm(){
      this.updateSb(this.sbLable)
      this.sbDuox = false;
    },

  },
}
</script>

<style scoped>

</style>
